package com.example.chenzixin.views;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatImageView;

import com.example.chenzixin.viewmodel.ChengyuDetailsViewModel;
import com.example.read.R;

public class ChengYuDetailsActivity extends AppCompatActivity {
    private ChengyuDetailsViewModel viewModel = new ChengyuDetailsViewModel();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chengyu_details);

        AppCompatImageView iv_back = findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        TextView tv_name = findViewById(R.id.tv_name);
        TextView tv_duyin = findViewById(R.id.tv_duyin);
        TextView tv_jieshi = findViewById(R.id.tv_jieshi);
        TextView tv_chuzi = findViewById(R.id.tv_chuzi);
        TextView tv_tongyi = findViewById(R.id.tv_tongyi);
        TextView tv_fanyi = findViewById(R.id.tv_fanyi);
        TextView tv_lizi = findViewById(R.id.tv_lizi);

        viewModel.event.observe(this,  details -> {
            tv_name.setText(details.getName());
            tv_duyin.setText("读音:" + details.getPronounce());
            tv_jieshi.setText("解释:" + details.getContent());
            tv_chuzi.setText("出自" +details.getComefrom());
//            StringBuilder fanyi = new StringBuilder();
//            for (int i = 0; i < details.getAntonym().size(); i++) {
//                fanyi.append(details.getAntonym().get(i));
//                if (i < details.getAntonym().size() - 1) {
//                    fanyi.append(",");
//                }
//            }
//            tv_fanyi.setText(fanyi);
            StringBuilder tongyi = new StringBuilder();
            for (int i = 0; i < details.getThesaurus().size(); i++) {
                tongyi.append(details.getThesaurus().get(i));
                if (i < details.getThesaurus().size() - 1) {
                    tongyi.append(",");
                }
            }
            tv_tongyi.setText("同义词：" + tongyi);
            tv_lizi.setText("例子：" + details.getExample());
        });

        viewModel.getDetails(getIntent().getStringExtra("text"));
    }
}